package club.calong.sanniu.util;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;


public class HttpUtils {

    public static  String get(String url, Map<String, String> queries) {

        RestTemplate client = new RestTemplate();
        ResponseEntity<String> response = client.getForEntity(url,  String.class,queries);
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("utf-8"));
        client.setMessageConverters(Collections.singletonList(converter));
        return response.getBody();
    }
    public static String get2 (String url, Map<String, String> queries)  {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string();
        }catch (Exception exception){
            exception.printStackTrace();
            return "{}";
        }
    }
}
